//
//  LGBallDetailFollowRecordView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/8/25.
//  药球详情--随访记录--随访意见

#import "LGBallDetailFollowRecordView.h"
#import "MCPlayer.h"
@interface LGBallDetailFollowRecordView()<MCPlayerDelegate>

/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UILabel *contentLabel;
/**   */
@property (nonatomic , strong)UIView *itemView;
/**   */
@property (nonatomic , strong)NSMutableArray *itemArray;
/**   */
@property (nonatomic , strong)UIButton *currentBtn;
@end

@implementation LGBallDetailFollowRecordView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
    }
    return self;
}

-(void)setTitle:(NSString *)title{
    _title = title;
    self.titleLabel.text = title;
}

-(void)setContent:(NSString *)content{
    _content = content;
    self.contentLabel.text = content;
    CGFloat top = (content.length>0)?viewPix(10):0;
//    [self.itemView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.contentLabel.mas_bottom).offset(top);
//    }];
}

-(void)setRecordArray:(NSArray *)recordArray{
    _recordArray = recordArray;
    for (UIView *view in self.itemArray) {
        [view removeFromSuperview];
    }
    [self.itemArray removeAllObjects];
    
    CGFloat contentHeight = [self.content textHeightWithFont:LGFont(14) maxWidth:Screen_W-viewPix(121)];
    CGFloat topHeight = (self.content.length>0)?viewPix(10)+contentHeight:contentHeight;
    if(recordArray.count == 0){
        [self.itemView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(0));
        }];
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(topHeight));
        }];
        return;
    }
    CGFloat top = 0;
    for (NSInteger i=0; i<recordArray.count; i++) {
        NSDictionary *tempDic = recordArray[i];
        NSString *content = LGNSString(tempDic[@"text"]);
        [self recordViewWithTop:top index:i content:content];
        CGFloat height = [content textHeightWithFont:LGFont(14) maxWidth:Screen_W-viewPix(156)];
        top = (height>viewPix(35))?(top+height+viewPix(8)):(top+viewPix(35)+viewPix(8));
    }
    [self.itemView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(top-viewPix(8)));
    }];
   
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(top+topHeight-viewPix(8)));
    }];
}

-(void)playRecordAction:(UIButton *)sender{
    NSInteger index = sender.tag-10000;
    NSString *url = LGNSString(self.recordArray[index][@"url"]);
    MCPlayer *player = [MCPlayer shareManager];
    [player playMediaWithUrl:url tempPath:nil desPath:nil delegate:self];
//    NSInteger sections = [self.recordArray[index][@"duration"] integerValue];
//    self.currentBtn = sender;
//    self.currentBtn.imageView.animationDuration = 1;
//    self.currentBtn.imageView.animationRepeatCount = sections;
//    [self.currentBtn.imageView startAnimating];
}

-(void)playerPlayTimeSecond:(CGFloat)seconds duration:(CGFloat)duration currentStr:(NSString *)currentString withResidueStr:(NSString *)residueStr player:(NSObject *)player{
    if(seconds == duration){
        //播放完成
//        [self.currentBtn.imageView stopAnimating];
    }
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    [self addSubview:self.titleLabel];
    [self addSubview:self.contentLabel];
    [self addSubview:self.itemView];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.equalTo(self).offset(viewPix(12));
    }];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(viewPix(76));
        make.right.equalTo(self).offset(-viewPix(12));
        make.top.equalTo(self.titleLabel);
    }];
    [self.itemView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.contentLabel.mas_bottom).offset(viewPix(10));
        make.left.right.equalTo(self.contentLabel);
        make.bottom.equalTo(self);
        make.height.equalTo(@(0));
    }];
}
-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#666666" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _titleLabel;
}

-(UILabel *)contentLabel{
    if(!_contentLabel){
        _contentLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _contentLabel;
}

-(UIView *)itemView{
    if(!_itemView){
        _itemView = [[UIView alloc]init];
    }
    return _itemView;
}

-(NSMutableArray *)itemArray{
    if(!_itemArray){
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

-(void)recordViewWithTop:(CGFloat)top index:(NSInteger)index content:(NSString *)content{
    UILabel *contentLabel = [UILabel lableWithText:content colorString:@"#333333" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:0];
    [self.itemView addSubview:contentLabel];
    [self.itemArray addObject:contentLabel];
    UIButton *playBtn = [UIButton buttonWithImage:@"药球管理-播放" selectImage:@"药球管理-播放"];
    [playBtn addTarget:self action:@selector(playRecordAction:) forControlEvents:UIControlEventTouchUpInside];
    playBtn.tag = 10000+index;
    [self.itemView addSubview:playBtn];
    [self.itemArray addObject:playBtn];
    
    UIImage *image1 = [UIImage imageNamed:@"voicePlay1"];
    UIImage *image2 = [UIImage imageNamed:@"voicePlay2"];
    UIImage *image3 = [UIImage imageNamed:@"voicePlay3"];
    playBtn.imageView.animationImages = @[image1,image2,image3];
    
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.itemView);
        make.top.equalTo(self.itemView).offset(top);
        make.right.equalTo(self.itemView).offset(-viewPix(36));
    }];
    [playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contentLabel);
        make.left.equalTo(contentLabel.mas_right);
        make.width.height.equalTo(@(viewPix(32)));
    }];
}

@end
